#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <unistd.h>
#include "socket.hpp"
#include <stdlib.h>
#include <poll.h>

#define NUM 128

void Usage(const std::string &proc)
{
    std::cout << "Usage: " << proc << " port" << std::endl;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    int listen_sock = Sock::Socket();
    uint16_t Port = (uint16_t)atoi(argv[1]);
    Sock::Bind(listen_sock, Port);
    Sock::Listen(listen_sock);
    struct pollfd rfd[NUM];
    //给数组的fd赋初值
    for(int i = 0;i < NUM;++i)
    {
        rfd[i].fd = -1;
    }
    rfd[0].fd = listen_sock;
    rfd[0].events = POLLIN;
    rfd[0].events = 0;
    int cnt = 1; //等待的fd数量
    for (;;)
    {
        int n = poll(rfd, cnt, -1);
        switch (n)
        {
        case -1:
            std::cout << "poll error" << std::endl;
            break;
        case 0:
            std::cout << "timeout..." << std::endl;
            break;
        default:
            for (int i = 0; i < cnt; ++i)
            {
                if(rfd[i].fd == -1)
                    continue;
                // 合法的fd
                if (rfd[i].revents & POLLIN)
                {
                    if (listen_sock == rfd[i].fd)
                    {
                        int sock = Sock::Accept(listen_sock);
                        if (sock >= 0)
                        {
                            // 服务器已经满载了
                            if (cnt == NUM)
                            {
                                std::cout << "服务器已经满载了！" << std::endl;
                                close(sock);
                            }
                            else
                            {
                                rfd[cnt].fd = sock;
                                rfd[cnt].events = POLLIN;
                                rfd[cnt].revents = 0;
                                ++cnt;
                            }
                        }
                    }
                    else
                    {
                        // recv/read
                        char buffer[1024] = {0};
                        ssize_t s = read(rfd[i].fd, buffer, sizeof(buffer) - 1);
                        if (s > 0)
                        {

                            std::cout << "client[" << rfd[i].fd << "]# " << buffer << std::endl;
                        }
                        // 关闭链接了
                        else if (s == 0)
                        {
                            close(rfd[i].fd);
                            rfd[i].fd = -1;
                            std::cout << rfd[i].fd << " 已经关闭了链接" << std::endl;
                        }
                        // 读取失败了
                        else
                        {
                            close(rfd[i].fd);
                            rfd[i].fd = -1;
                            std::cout << "读取数据失败 " << std::endl;
                        }
                    }
                }
            }
            break;
        }
    }

    return 0;
}